home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / SafeLauncher 1.0 / Source / SafeLauncher.c next >
Encoding:
C/C++ Source or Header  |  1994-10-12  |  6.5 KB  |  253 lines  |  [TEXT/MMCC]

  1. //
  2. // ProperLauncher.c
  3. //
  4. //    Written by Nick Triantos
  5. //
  6. //    Copyright © 1994 by Nick Triantos.  All rights reserved.
  7. //
  8. //    This program will launch the one named in the 'STR ' resource if and only
  9. //    if we're in 24-bit addressing mode.  Prevents crashes for some old great
  10. //    games.
  11. //
  12. //    Modification history: (most recent first)
  13. //
  14. //        Date:        Chg:    Description:
  15. //        10/11/94    0000    Initial coding.  Make checks for 32- and 24-bit addr,
  16. //                            use rsrc. for editability, Sys 7 dependent
  17. //        10/11/94    0001    Add check for Quadra's cache.  Some apps need for it
  18. //                            to be off.
  19.  
  20. #define kDialogID                128
  21. #define kDialogItemNo            2
  22. #define kDialogFont                1
  23. #define kDialogSize                10
  24. #define kFilename                128
  25. // Error strings
  26. #define kFail24BitAddrID        129
  27. #define kFail32BitAddrID        130
  28. #define kFailQuadCacheOffID        131
  29. #define kFailBadDepthID            132
  30. #define kFailFileNotFoundID        133
  31. #define kLPrpID                    0
  32. // Bits read from the 'LPrp' (Launch Properties) resource
  33. #define bitCheck32BitAddr        0x80
  34. #define bitCheck24BitAddr        0x40
  35. #define bitDisableQuadCache        0x20
  36. #define bitSetDepth                0x10
  37.  
  38.  
  39. typedef struct LaunchProperties {
  40.     unsigned char        chkBits ;
  41.     unsigned char        depth ;
  42. } LaunchProperties, *LaunchPropPtr, **LaunchPropHdl ;
  43.  
  44.  
  45. #include <GestaltEqu.h>            // To check for sys info for flags
  46. #include <Processes.h>            // To launch the app
  47. #include <Palettes.h>            // For depth-setting routines
  48. #ifndef __TRAPS__
  49. #include <Traps.h>
  50. #endif
  51. #ifndef __MWERKS__
  52. pascal OSErr SetDialogDefaultItem (DialogPtr theDialog,
  53.         short newItem) = {0x303C,0x0304,0xAA68};
  54. #endif
  55.  
  56.  
  57.  
  58. // local functions
  59. void InitToolbox( void ) ;
  60. pascal void myItem( WindowPtr theWindow, short theItem ) ;
  61. void DisplayFailAlert( short id ) ;
  62. void LaunchApp( void ) ;
  63. Boolean HWPrivAvailable( void ) ;
  64. // external functions
  65. Boolean TrapAvailable(short theTrap) ;
  66.  
  67.  
  68. Rect        gItemRect ;
  69. short        gStrID ;
  70.  
  71.  
  72. void InitToolbox( void )
  73. {
  74.     InitGraf( &qd.thePort ) ;
  75.     InitFonts() ;
  76.     InitWindows() ;
  77.     InitMenus() ;
  78.     TEInit() ;
  79.     InitDialogs( nil ) ;
  80.     InitCursor() ;
  81. }
  82.  
  83. main()
  84. {
  85.     OSErr            theErr ;
  86.     long            result ;
  87.     LaunchPropHdl    lpHdl ;
  88.     LaunchPropPtr    lpPtr ;
  89.     
  90.     MaxApplZone() ;
  91.     InitToolbox() ;
  92.     lpHdl = (LaunchPropHdl)GetResource( 'LPrp', kLPrpID ) ;
  93.     if (lpHdl == nil)
  94.         return 0 ;
  95.     HLock( (Handle)lpHdl ) ;
  96.     lpPtr = *lpHdl ;
  97.     if ( lpPtr->chkBits & (bitCheck32BitAddr|bitCheck24BitAddr) ) {
  98.         theErr = Gestalt( gestaltAddressingModeAttr, &result ) ;
  99.         if (theErr != noErr)
  100.             return theErr ;
  101.         // Check if we're in 24-bit addressing mode
  102.         if ((lpPtr->chkBits & bitCheck24BitAddr) &&
  103.             (BitTst(&result, 31-gestalt32BitAddressing)==TRUE) ) {
  104.             DisplayFailAlert( kFail24BitAddrID ) ;
  105.             goto exit ;
  106.         }
  107.         // Check if we're in 32-bit addressing mode
  108.         if ((lpPtr->chkBits & bitCheck32BitAddr) &&
  109.             (BitTst(&result, 31-gestalt32BitAddressing)==FALSE) ) {
  110.             DisplayFailAlert( kFail32BitAddrID ) ;
  111.             goto exit ;
  112.         }
  113.     }
  114.     // Check if the Quadra cache is off
  115.     if (lpPtr->chkBits & bitDisableQuadCache) {
  116.         Boolean        oldState ;
  117.         Boolean        tempState = TRUE ;        // True or False, it makes no difference.
  118.         
  119.         if (!HWPrivAvailable())
  120.             goto exit ;
  121.         // Data cache
  122.         oldState = SwapDataCache( tempState ) ;    // Get the state in "oldState"
  123.         if (oldState != tempState)
  124.             (void)SwapDataCache( oldState ) ;    // Restore state if we changed it
  125.         if (oldState == TRUE) {
  126.             DisplayFailAlert( kFailQuadCacheOffID ) ;
  127.             goto exit ;
  128.         }
  129.         // instruction cache
  130.         oldState = SwapInstructionCache( tempState ) ;    // Get the state in "oldState"
  131.         if (oldState != tempState)
  132.             (void)SwapInstructionCache( oldState ) ;    // Restore state if we changed it
  133.         if (oldState == TRUE) {
  134.             DisplayFailAlert( kFailQuadCacheOffID ) ;
  135.             goto exit ;
  136.         }
  137.     }
  138.     // Set bit depth if we need to
  139.     if (lpPtr->chkBits & bitSetDepth) {
  140.         short whichFlags=0, flags=0 ;
  141.         short depthID = HasDepth( GetGDevice(), lpPtr->depth, 0, 0 ) ;
  142.         
  143.         if (depthID == 0) {
  144.             DisplayFailAlert( kFailBadDepthID ) ;
  145.             goto exit ;
  146.         }
  147.         else {
  148.             if (lpPtr->depth == 1)
  149.                 whichFlags = 1 ;    // See "SetDepth" in THINK Ref...
  150.             SetDepth( GetGDevice(), lpPtr->depth, whichFlags, flags ) ;
  151.         }
  152.     }
  153.     LaunchApp() ;
  154. exit:
  155.     HUnlock( (Handle)lpHdl ) ;
  156.     ReleaseResource( (Handle)lpHdl ) ;
  157.     return 0 ;
  158. }
  159.  
  160.  
  161. pascal void myItem( WindowPtr theWindow, short theItem )
  162. {
  163.     StringHandle strHdl ;
  164.     StringPtr theStr ;
  165.     
  166.     strHdl = GetString( gStrID ) ;
  167.     if (strHdl == NULL)
  168.         return ;
  169.     HLock( (Handle)strHdl ) ;
  170.     theStr = *strHdl ;
  171.     TextFont( kDialogFont ) ;
  172.     TextSize( kDialogSize ) ;
  173.     #ifdef __MWERKS__
  174.         TETextBox( &theStr[1], theStr[0], &gItemRect, teFlushDefault ) ;
  175.     #else
  176.         TextBox( &theStr[1], theStr[0], &gItemRect, teFlushDefault ) ;
  177.     #endif
  178.     HUnlock( (Handle)strHdl ) ;
  179.     ReleaseResource( (Handle)strHdl ) ;
  180. }
  181.  
  182. void DisplayFailAlert( short id )
  183. {
  184.     DialogPtr theDlg ;
  185.     short result ;
  186.     short iType ;
  187.     Rect iRect ;
  188.     Handle iHandle ;
  189.     
  190.     gStrID = id ;
  191.     theDlg = GetNewDialog( kDialogID, NULL, (WindowPtr)-1 ) ;
  192.     GetDItem( theDlg, kDialogItemNo, &iType, &iHandle, &iRect ) ;
  193.     gItemRect = iRect ;
  194.     SetDItem( theDlg, kDialogItemNo, iType, (Handle)myItem, &iRect ) ;
  195.     SetDialogDefaultItem( theDlg, ok ) ;
  196.     ShowWindow( (WindowPtr)theDlg ) ;
  197.     ModalDialog( NULL, &result ) ;
  198.     DisposeDialog( theDlg ) ;
  199. }
  200.  
  201.  
  202. void LaunchApp( void ) 
  203. {
  204.     LaunchParamBlockRec        myLaunchParams ;
  205.     ProcessSerialNumber        launchedProcessSN ;
  206.     OSErr                    launchErr ;
  207.     long                    prefSize ;
  208.     long                    minSize ;
  209.     long                    availSize ;
  210.     FSSpec                    sfFile ;
  211.     short                    vRefNum ;
  212.     long                    dirID ;
  213.     StringHandle            theStr ;
  214.     StringPtr                filename ;
  215.     
  216.     theStr = GetString( kFilename ) ;
  217.     if (theStr == nil)
  218.         return ;
  219.     HLock( (Handle)theStr ) ;
  220.     filename = *theStr ;
  221.     HGetVol( nil, &vRefNum, &dirID ) ;
  222.     launchErr = FSMakeFSSpec( 0, dirID, filename, &sfFile ) ;
  223.     HUnlock( (Handle)theStr ) ;
  224.     ReleaseResource( (Handle)theStr ) ;
  225.     myLaunchParams.launchBlockID = extendedBlock ;
  226.     myLaunchParams.launchEPBLength = extendedBlockLen ;
  227.     myLaunchParams.launchFileFlags = 0 ;
  228.     myLaunchParams.launchControlFlags = launchContinue + launchNoFileFlags ;
  229.     myLaunchParams.launchAppSpec = &sfFile ;
  230.     myLaunchParams.launchAppParameters = nil ;
  231.  
  232.     launchErr = LaunchApplication( &myLaunchParams ) ;
  233.  
  234.     if (launchErr == fnfErr)
  235.         DisplayFailAlert( kFailFileNotFoundID ) ;
  236.     // These aren't used, but what the hey...they're neat to have.
  237.     prefSize = myLaunchParams.launchPreferredSize ;
  238.     minSize = myLaunchParams.launchMinimumSize ;
  239.     if ( !launchErr )
  240.         launchedProcessSN = myLaunchParams.launchProcessSN ;
  241.     else if ( launchErr == memFullErr )
  242.         availSize = myLaunchParams.launchAvailableSize ;
  243. }
  244.  
  245.  
  246. // This function is needed to call SwapDataCache/SwapInstructionCache
  247. Boolean HWPrivAvailable(void)
  248. {
  249.     return TrapAvailable(_HWPriv);
  250. }
  251.  
  252.  
  253.